/**
 *
 * 用于控制三维场景中的模型的显隐关系和透明度设置ShowOpacity.js
 *
 * Daniel
 *
 * 2018.03.06
 * **/

define(function(){
    var ShowOpacity = function(){
        
        this.opacityDataid = [];            //透明度模型id收纳器
        this.opacityData = [];              //透明度模型容器
    
        this.visibleDataid = [];            //显隐模型id收纳器
        this.showModelArray = [];               //显隐模型容器组
        
        this.visibleData = [];
        
        this.frame = {
            wireframeDataid:[],
            wireframeData:[],
            
        };
        var scope = this;
        
    };
    
    //模型透明度设置
    ShowOpacity.prototype.opacityControl = function(object3d,value){
        if (object3d !== undefined)
        {
            var id = object3d.id;
            var material = object3d.material;
            if(material.constructor == Array){
                var len = material.length;
                for(var i = 0; i < len; i++){
                    var matObj = material[i];
                    if(value == 1){
                        matObj.opacity = value;
                        matObj.transparent = false;
                    }else{
                        matObj.opacity = value;
                        matObj.transparent = true;
                    }
                }
            }else{
                if(value == 1){
                    material.transparent = false;
                    material.opacity = value;
                }else{
                    material.transparent = true;
                    material.opacity = value;
                }
                
            }
            if (this.opacityDataid.indexOf(id) == -1){
                this.opacityDataid.push(id);
                this.opacityData.push(object3d);
            }
        } else
        {
            console.error("未找到构件！");
        }
    };
    
    //透明度重置
    ShowOpacity.prototype.opacityRest = function(){
        var opacityData = this.opacityData;
        if(opacityData.length == 0)return;
        var opacityDataid = this.opacityDataid;
        this.opacityset(opacityData,opacityDataid);
    };
    
    //透明度恢复
    ShowOpacity.prototype.opacityset = function (objarray,idarray){
        if(objarray == undefined )return;
        var len = objarray.length;
             idarray = [];
        for(var i = 0; i < len; i++){
            var obj = objarray[i];
            this.opacityControl(obj,1);
        }
    };
    
    //显隐控制器
    ShowOpacity.prototype.hoverControl = function (){
        var scope = this;
        return function (object) {
            if (object !== undefined){
                var id = object.id;
                var tage = scope.visibleDataid.indexOf(id);
                if (tage == -1)
                {
                    object.visible = false;
                    scope.visibleDataid.push(id);
                    scope.showModelArray.push(object);
            
                } else
                {
                    object.visible = true;
                    scope.visibleDataid.splice(tage, 1);
                }
            }
            else
            {
                return
            }
        }
    };
    
    //显隐恢复
    ShowOpacity.prototype.showAll = function (){
        
        var scope = this;
        return function(){
            var len = scope.showModelArray.length;
            if(len == 0)return;
            for(var i = 0; i < len; i++){
                var object = scope.showModelArray[i];
                object.visible = true;
            }
            scope.showModelArray = [];
            scope.visibleData = [];
            scope.visibleDataid = [];
        };
        
      
    };
    
    //线框显示/隐藏
    ShowOpacity.prototype.showFrame = function (){
        var scope = this;
        return function (object3d) {
            if (object3d !== undefined){
                var id = object3d.id;
                var flag = scope.frame.wireframeDataid.indexOf(id);
                var material = object3d.material;
                if (flag == -1){
                    material.wireframe = true;
                    scope.frame.wireframeDataid.push(id);
                    scope.frame.wireframeData.push(object3d);
                } else{
                    material.wireframe = false;
                    scope.frame.wireframeDataid.splice(flag, 1);
                    scope.frame.wireframeData.splice(flag, 1);
            
                }
            } else{
                console.error("未找到指定模型");
            }
        };
    };
    
    //恢复所有已标记线框
    ShowOpacity.prototype.resetFrame = function () {
        var scope = this;
        return function () {
            var objectArr = scope.frame.wireframeData;
            var length = scope.frame.wireframeData.length;
            var i = 0;
            for(;i<length; i++){
                debugger;
                var model = objectArr[i];
                    model.material.wireframe = false;
            }
            scope.frame.wireframeDataid = [];
            scope.frame.wireframeData = [];
            
        };
    };
    
    return ShowOpacity;
});